package com.action;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;

import com.bussiness.BussinessContactImpl;
import com.dao.Contact;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ContactAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6021259894811039943L;

	private Contact contact;
	private List<Contact> contactList;
	private Long id;
	private BussinessContactImpl business;
	private boolean inActive;
	private String isActive;
	//private Long id;
	private String firstName;
	private String lastName;
	private String emailId;
	private String cellNo;
	private Date birthDate;
	private String website;
	private String contactType;
	private Long contactManager;
//	private String isActive;

	private int startPage;
	private int endPage;
	private String order;
	private int currentPage;
	
	private String chooseContactType;
	
	
	
	public BussinessContactImpl getBusiness() {
		return business;
	}
	public void setBusiness(BussinessContactImpl business) {
		this.business = business;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	public String getCellNo() {
		return cellNo;
	}
	public void setCellNo(String cellNo) {
		this.cellNo = cellNo;
	}
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	public String getWebsite() {
		return website;
	}
	public void setWebsite(String website) {
		this.website = website;
	}
	public String getContactType() {
		return contactType;
	}
	public void setContactType(String contactType) {
		this.contactType = contactType;
	}
	public Long getContactManager() {
		return contactManager;
	}
	public void setContactManager(Long contactManager) {
		this.contactManager = contactManager;
	}
	public ContactAction(){
		business = new BussinessContactImpl();
	}
	public Contact getContact() {
		return contact;
	}
	public void setContact(Contact contact) {
		this.contact = contact;
	}
	public List<Contact> getContactList() {
		return contactList;
	}
	public void setContactList(List<Contact> contactList) {
		this.contactList = contactList;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		if(order.equals("First Name")){
			this.order = "order by firstname";
		}
		else if(order.equals("Last Name")){
			this.order = "order by lastname";
		}
		else if(order.equals("Email")){
			this.order = "order by emailId";
		}
		else if(order.equals("Cell Phone")){
			this.order = "order by cell_no";
		}
		else if(order.equals("Active")){
			this.order = "order by active";
		}
		else if(order.equals("Birth Date")){
			this.order = "order by birthdate";
		}else{
		this.order = "";
		}
	}
	public String edit(){
		contact.setContactType(chooseContactType);
		contact.setIsActive(isActive);
		if(business.editAction(contact)){
		return SUCCESS;}
		else{
			return ERROR;
		}
	}
	
	public String listEditContact(){
		try{
		//System.out.println("77777777"+ getId());	
		contactList = business.listAllContactAction();
		contact = business.listEditContactAction(getId());
			return SUCCESS;
		} catch(HibernateException e){
			return ERROR;
		}
	}
	
	public String delete(){
		if(business.deleteAction(getId())){
			return SUCCESS;}
			else{
				return ERROR;
			}
	}
	
	public String search(){
		try{
			if(getCurrentPage() == 0){
				setCurrentPage(1);
			}
			//System.out.println("+++++++++++++++"+getInActive());
			
			//System.out.println("99999999999999999999"+getCurrentPage());
			startPage = 5*(getCurrentPage()-1);
			endPage = startPage + 5;
			
			
			System.out.println(contact.getFirstName()+"--"+contact.getLastName()+"--"+getInActive()+"--"+getOrder()+"--"+startPage+"--"+endPage);
			
			
			//ActionContext.getContext().getParameters();
			
			contactList = business.searchAction(contact.getFirstName(),contact.getLastName(),getInActive(),getOrder(),startPage,endPage);
				return SUCCESS;
			} catch(HibernateException e){
				return ERROR;
			}
	}
	
	public String add(){
		//System.out.println("*******************"+getIsActive());
		
		contact.setIsActive(isActive);
		contact.setContactType(chooseContactType);
		if(business.addAction(contact)){
			return SUCCESS;}
			else{
				return ERROR;
			}
	}
	
	public String create(){
		contactList = business.listAllContactAction();
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
try{
			
			contactList = business.listAllContactAction();
				return SUCCESS;
			} catch(HibernateException e){
				return ERROR;
			}
	}
	public boolean getInActive() {
		return inActive;
	}
	public void setInActive(boolean inActive) {
		this.inActive = inActive;
	}
	public String getIsActive() {
		return isActive;
	}
	public void setIsActive(String isActive) {
		this.isActive = isActive;
	}
	public String getChooseContactType() {
		return chooseContactType;
	}
	public void setChooseContactType(String chooseContactType) {
		this.chooseContactType = chooseContactType;
	}
	
	
}
